package com.example.demo.util;

import java.util.Random;

/**
 * *  Author:zlf
 * *  2020/10/28 15:13
 **/
public class RandomPwd {

    private static String numbers = "0123456789";

    private static String lowerLetters = "abcdefghijklmnopqrstuvwxyz";

    private static String upperLetters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

    private static String symbols = "~!@#$%^&*()_+<>?";

    private static Random random = new Random();

    //type 1 纯数字 , 2 数字+字母 , 3 数字，大小写字母+符号
    public static String getPwd(int len, int type) {
        char[] pwd = new char[len];
        String params = "";
        switch (type) {
            case 1:
                params = numbers;
                break;
            case 2:
                params = numbers + lowerLetters;
                break;
            case 3:
                params = numbers + lowerLetters + upperLetters + symbols;
                break;
            default:
                params = numbers + lowerLetters + symbols;
                break;
        }
        for (int i = 0; i < len; i++) {
            pwd[i] = getRandomChar(params);
        }
        return String.valueOf(pwd);
    }

    private static char getRandomChar(String param) {
        return param.charAt(random.nextInt(param.length()));
    }
}
